package team.chess.client;

import team.chess.util.Request;
import team.chess.util.Response;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client extends Thread{

    public String host_account;
    public String iP;
    public Socket socket;
    public int flag;
    OutputStream os = null;
    ObjectOutputStream oos = null;

    public Client(String host_account, String iP){
        this.host_account = host_account;
        this.iP = iP;


    }

    @Override
    public void run() {

        socket = new Socket();
        try {
            while(!socket.isConnected()){
                socket.connect(new InetSocketAddress(iP,6666));
            }
            System.out.println(socket.isConnected());
            System.out.println("连接服务端成功 连接账户"+host_account+"    ip地址："+iP);
            this.flag = 1;

            os = socket.getOutputStream();
            oos=new ObjectOutputStream(os);

        } catch (IOException e) {
            System.out.println("连接服务端失败 连接账户"+host_account+"    ip地址："+iP);
            e.printStackTrace();
        }
    }


    public void send(Request request){

        try {
            oos.writeObject(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
